<template>
  <div id="search">
    <el-form class="form-flex" label-width="auto">
      <el-form-item label="送水时间">
        <el-col :span="11">
          <el-date-picker
            v-model="listQuery.startTime"
            type="date"
            aria-label="Pick a startTime"
            placeholder="Pick a startTime"
            style="width: 100%"
            value-format="YYYY-MM-DD"
          />
        </el-col>
        <el-col class="text-center" :span="1" style="margin: 0 0.5rem"
          >-</el-col
        >
        <el-col :span="11">
          <el-date-picker
            v-model="listQuery.endTime"
            aria-label="Pick an endTime"
            placeholder="Pick an endTime"
            style="width: 100%"
            value-format="YYYY-MM-DD"
          />
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search"> 查询 </el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div id="list">
    <el-table :data="history" stripe style="width: 100%">
      <el-table-column prop="id" label="编号" />
      <el-table-column prop="custName" label="用户名称" />
      <el-table-column prop="workerName" label="送水工名称" />
      <el-table-column prop="sendWaterTime" label="送水时间" />
      <el-table-column prop="sendWaterCount" label="送水数量" />
    </el-table>
  </div>

  <!-- 底部分页导航 -->
  <el-pagination
    v-model:current-page="listQuery.pageNum"
    v-model:page-size="listQuery.pageSize"
    :page-sizes="[2, 3, 5, 10]"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
</template>

<script>
import { mapState } from 'vuex';
export default {
  data() {
    return {
      history: [],
      listQuery: {
        userName: null,
        pageNum: 1,
        pageSize: 5,
        startTime: null,
        endTime: null,
      },
      total: 0,
    };
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    console.info("User is:",this.user)
    this.listQuery.userName = this.user.userName; // 获取user中的userName的值
    this.getHistory();
  },
  methods: {
    getHistory() {
      this.$axios
        .get("http://localhost:8091/history/list2", {
          params: this.listQuery,
          // 封装分页参数给后端
          // 封装开始时间和结束时间
        })
        // 请求成功后调用
        .then((res) => {
          console.log(res);
          let code = res.data.code;
          if (code === 200) {
            this.history = res.data.data.list;
            this.total = res.data.data.total;
          }
        })
        // 请求失败后调用
        .catch((err) => {
          console.info(`请求失败${err}`);
          this.$message.error(`请求失败${err}`);
        });
    },
    handleSizeChange(val) {
      // 设置新的每页条数
      this.pageSize = val;
      // 当前页码默认是第一页
      this.pageNum = 1;
      // 重新进行分页查询
      this.getHistory();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getHistory();
    },
    search() {
      console.info(`开始时间是${this.listQuery.startTime}`);
      console.info(`结束时间是${this.listQuery.endTime}`);
      this.getHistory();
    },
    reset() {
      this.listQuery.startTime = null;
      this.listQuery.endTime = null;
      this.getHistory();
    },
  },
};
</script>

<style lang="scss" scoped>
#search {
  display: flex;
  justify-content: space-between; /* 在主轴（默认水平方向）上分配空间 */
  align-items: center;
  .form-flex {
    display: flex;
    flex: 1; /* 让表单占据剩余的空间 */
    justify-content: center;
    align-items: center;
  }
  #addButton {
    margin-left: auto;
  }
}
</style>
